<template>
  <div id="Home">
    <el-backtop></el-backtop>
    <!-- Banner图 -->
    <banner />

    <!-- 推荐歌单 -->
    <div class="playlist w">
      <div class="title">推荐歌单</div>
      <ul v-loading="loading">
        <li
          v-for="(item, index) in playlist"
          :key="index"
          @click="change(item.id)"
        >
          <!-- 歌单图和播放量 -->
          <div class="top">
            <div class="img">
              <!-- 专辑图片 -->
              <img :src="item.picUrl" alt="" />
              <!-- 播放量 -->
              <div class="count">
                <i
                  class="el-icon-caret-right"
                  style="color: #fff; font-size: 14px;"
                ></i>
                <span>{{ item.playCount | formatCount }}</span>
              </div>
            </div>
          </div>
          <!-- 描述文字 -->
          <div class="bottom">{{ item.name }}</div>
        </li>
      </ul>
    </div>

    <!-- 推荐歌曲 -->
    <div class="Handlebars w">
      <div class="title">推荐歌曲</div>
      <div class="container">
        <div class="left">
          <ul>
            <li
              v-for="(item, index) in newmusic"
              :key="index"
              @click="Musicid(item.id)"
            >
              <!-- 歌曲图片 -->
              <img :src="item.picUrl" alt="" />
              <span>
                <i class="el-icon-video-play"></i>
              </span>
              <!-- 歌曲名字-歌手 -->
              <div class="box">
                <div class="musicname">
                  {{ item.name }}
                </div>

                <div class="name">{{ item.song.artists[0].name }}</div>
              </div>
              <div class="time">{{ item.song.duration }}</div>
            </li>
          </ul>
        </div>
      </div>
    </div>

    <!-- 推荐歌手 -->
    <div class="singer w">
      <div class="title">推荐歌手</div>
      <ul class="container" v-loading="loading">
        <li v-for="(item, index) in singer" :key="index">
          <img :src="item.img1v1Url" alt="" />
          <div class="name">{{ item.name }}</div>
          <div class="Singlenum">单曲数{{ item.musicSize }}</div>
        </li>
      </ul>
      <div class="statement">本站仅供学习使用--万能青年</div>
    </div>
  </div>
</template>

<script>
// 导入轮播图
import banner from "../../components/banner/Banner";

// 导入首页网络请求
import { playlist, newmusic, singer, musicurl } from "../../network/index";
import Bus from "../../Bus";
export default {
  name: "Home",
  components: {
    banner,
    playlist,
    newmusic,
    singer,
    musicurl,
  },
  data() {
    return {
      // 推荐歌单数据
      playlist: [],
      // 推荐新歌曲数据
      newmusic: [],
      // 推荐歌手数据
      singer: [],

      musicid: 0,
      musicurl: "",
      loading: true,
    };
  },
  async created() {
    // 获取推荐歌单
    await playlist().then((res) => {
      if (res.data.code == "200") {
        this.playlist = res.data.result;
        this.loading = false;
      }
    });
    // 获取推荐歌曲
    await newmusic().then((res) => {
      if (res.data.code == "200") {
        this.newmusic = res.data.result;
        console.log(this.newmusic);
      }
    });

    // 获取推荐歌手
    await singer().then((res) => {
      console.log(res);
      if (res.data.code == "200") {
        this.singer = res.data.artists;
      }
      this.loading = false;
    });
  },
  methods: {
    change(id) {
      this.$router.push({
        //核心语句
        path: "/playlistitem", //跳转的路径
        query: {
          //路由传参时push和query搭配使用 ，作用时传递参数
          id: id,
        },
      });
    },

    Musicid(id) {
      this.musicid = id;
      // 获取歌曲url
      musicurl(this.musicid).then((res) => {
        console.log(res);
        if (res.data.code == "200") {
          this.musicurl = res.data.data[0].url;
          // 发送音乐url给music组件
          Bus.$emit("Sendmusic", this.musicurl);
        }
      });
    },
  },
};
</script>

<style lang="less" scoped>
// 推荐歌单样式
#Home {
  background: url(../../assets/img/wallhaven-g75r7d.jpg) no-repeat;
  background-size: 100%;
}
.playlist {
  .title {
    padding-left: 10px;
    font-size: 16px;
    color: #000;
    font-weight: 700;
    margin: 20px 0;
  }
  ul {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
    li {
      width: 128px;
      height: 180px;
      padding: 10px 10px;
      cursor: pointer;
      .top .img {
        position: relative;
        margin-bottom: 20px;

        img {
          width: 100%;
          height: 122px;
          box-sizing: border-box;
          border-radius: 10%;
          box-shadow: 10px 10px 5px #ccc;
        }
        .count {
          position: absolute;
          top: 5px;
          font-weight: 700;
          right: 10px;
          color: #fff;
          background-color: #000;
        }
      }
      .bottom {
        font-size: 14px;
        font-weight: 700;
        width: 100%;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
        overflow: hidden;
      }
    }
    li:hover img {
      border: 2px solid #fa2800;
    }
  }
}
// 推荐歌单样式结束

// 推荐歌曲样式
.curr {
  display: block !important;
}
.Handlebars {
  .title {
    padding-left: 10px;
    font-size: 16px;
    color: #000;
    font-weight: 700;
    margin: 20px 0;
  }
  .container {
    .left {
      ul {
        display: flex;
        flex-wrap: wrap;
        li {
          display: flex;
          width: 580px;
          height: 70px;
          margin-top: 20px;
          padding-left: 10px;
          cursor: pointer;
          img {
            width: 70px;
            height: 70px;
          }
          span {
            position: absolute;
            // position: relative;
            width: 70px;
            height: 70px;
            background: rgba(0, 0, 0, 0.5);
            // display: flex;
            // justify-content: center;
            // align-items: center;
            display: none;
            i {
              position: absolute;
              top: 37%;
              left: 37%;
              color: #fa2800;
              font-size: 18px;
            }
          }

          .box1:hover {
            display: block;
          }
          .box {
            flex: 1;
            padding-left: 15px;
            .musicname {
              font-size: 18px;
              color: #000;
              font-weight: 700;
              margin-top: 10px;
            }
            .name {
              font-size: 14px;
              color: #666;
              margin-top: 10px;
            }
          }
          .time {
            padding-right: 35px;
            line-height: 70px;
          }
        }
        li:hover span {
          display: block;
        }
      }
    }
  }
}
// 推荐歌曲样式结束

// 推荐歌手样式
.singer {
  .title {
    padding-left: 10px;
    font-size: 16px;
    color: #000;
    font-weight: 700;
    margin: 20px 0;
  }
  .container {
    display: flex;
    flex-wrap: wrap;
    li {
      display: flex;
      flex-direction: column;
      width: 120px;
      height: 160px;
      justify-content: space-between;
      padding-top: 15px;
      cursor: pointer;

      align-items: center;
      img {
        width: 93px;
        height: 93px;
        border-radius: 50%;
        box-sizing: border-box;
      }
      .name {
        font-size: 16px;
        font-weight: 700;
        margin-top: 7px;
        word-break: break-all;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 1;
        overflow: hidden;
      }
      .Singlenum {
        font-size: 14px;
        margin-top: 7px;
        color: #fa2800;
      }
    }
    li:hover img {
      border: 2px solid #fa2800;
    }
  }
  .statement {
    color: #666;
    font-size: 14px;
    text-align: center;
    margin: 20px auto;
  }
}
</style>
